package com.itqf.controller;

import com.alibaba.fastjson.JSONObject;
import com.itqf.entity.User;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

/**
 * @Description:
 * @Company: 千锋互联
 * @Author: 李丽婷
 * @Date: 2021/1/21
 * @Time: 下午3:28
 */
@RestController  //@Controller+@ResponseBody
@CrossOrigin("http://localhost:63343")//允许该地址跨域访问该controller中的方法
public class MyRestController {//8080

    //get+ /user/1

   // @RequestMapping(value = "/user/{id}",method = RequestMethod.GET)
    //等价
    @GetMapping("/user/{id}")
    public User find(@PathVariable int id){
        System.out.println("查。。。。。");

        User user = new User();
        user.setId(id);
        user.setUsername("赵人和");
        user.setBirthdate(new Date());
        user.setSex("男");
        return  user;
    }
    //delete + /user/1
    @DeleteMapping("/user/{id}")
    public JSONObject delete(@PathVariable int id){
        System.out.println("删除。。。。。"+id);

        JSONObject jsonObject = new JSONObject();
        jsonObject.put("code",0);
        jsonObject.put("msg","删除成功");

        return  jsonObject;
    }

    //@RequestMapping(value = "/user",method = RequestMethod.PUT)
    @PutMapping("/user")
    public JSONObject update(@RequestBody User user){
        System.out.println("修改。。。。。"+user);

        JSONObject jsonObject = new JSONObject();
        jsonObject.put("code",0);
        jsonObject.put("msg","修改成功");
        jsonObject.put("data",user);

        return  jsonObject;
    }

    @PostMapping("/user")
    public JSONObject add(@RequestBody User user){
        System.out.println("新增。。。。。"+user);

        JSONObject jsonObject = new JSONObject();
        jsonObject.put("code",0);
        jsonObject.put("msg","新增成功");
        jsonObject.put("data",user);

        return  jsonObject;
    }

}
